# Configure the HuaweiCloud Provider
terraform {
  required_providers {
    huaweicloud = {
      source = "local-registry/huaweicloud/huaweicloud"
      version = "= 1.34.1"
    }
  }
}

provider "huaweicloud" {
  region     ="cn-north-4"
  access_key = ""
  secret_key = ""
}

data "huaweicloud_compute_instances" "test" {
  status        = "ACTIVE"
}

data "huaweicloud_csms_secret_version" "version_1" {
  secret_name = var.secretName
}

locals {
  ecs_ids = data.huaweicloud_compute_instances.test.instances[*].id
}

# Create a SMN Topic
resource "huaweicloud_smn_topic" "test" {
  name        = "${var.template_name}-smn"
}

data "template_file" "user_data" {
  template = file("./template")

   vars = {
    secretName=data.huaweicloud_csms_secret_version.version_1.secret_text
  }
}

# Create a Function resource
resource "huaweicloud_fgs_function" "test" {
  name        = "${var.template_name}-function"
  app         = "default"
  handler     = "index.handler"
  memory_size = 128
  timeout     = 3
  runtime     = "Python3.6"
  code_type   = "inline"

  func_code = "${data.template_file.user_data.rendered}"
}

# Create a trigger 
resource "huaweicloud_fgs_trigger" "test" {
  function_urn = huaweicloud_fgs_function.test.urn
  type         = "SMN"
  status       = "ACTIVE"

  smn {
    topic_urn = huaweicloud_smn_topic.test.topic_urn
  }
}

# Create a Cloud Eye alarm rule resource to cpu
resource "huaweicloud_ces_alarmrule" "test_cpu" {
  count = length(local.ecs_ids)
  alarm_name = "${var.template_name}-ces-cpu"

  metric {
    namespace   = "SYS.ECS"
    metric_name = "cpu_util"
    dimensions {
      name  = "instance_id"
      value = local.ecs_ids[count.index]
    }
  }
  condition {
    period              = 1
    filter              = "average"
    comparison_operator = ">"
    value               = var.condition_cpuValue
    unit                = "%"
    count               = 1
  }
  alarm_actions {
    type              = "notification"
    notification_list = [huaweicloud_smn_topic.test.id]
  }
}

# Create a Cloud Eye alarm rule resource to mem
resource "huaweicloud_ces_alarmrule" "test_mem" {
  count = length(local.ecs_ids)
  alarm_name = "${var.template_name}-ces-mem"

  metric {
    namespace   = "SYS.ECS"
    metric_name = "mem_util"
    dimensions {
      name  = "instance_id"
      value = local.ecs_ids[count.index]
    }
  }
  condition {
    period              = 1
    filter              = "average"
    comparison_operator = ">"
    value               = var.condition_memValue
    unit                = "%"
    count               = 1
  }
  alarm_actions {
    type              = "notification"
    notification_list = [huaweicloud_smn_topic.test.id]
  }
}

# Create a Cloud Eye alarm rule resource to disk
resource "huaweicloud_ces_alarmrule" "test_disk" {
  count = length(local.ecs_ids)
  alarm_name = "${var.template_name}-ces-disk"

  metric {
    namespace   = "SYS.ECS"
    metric_name = "disk_util_inband"
    dimensions {
      name  = "instance_id"
      value = local.ecs_ids[count.index]
    }
  }
  condition {
    period              = 1
    filter              = "average"
    comparison_operator = ">"
    value               = var.condition_diskValue
    unit                = "%"
    count               = 1
  }
  alarm_actions {
    type              = "notification"
    notification_list = [huaweicloud_smn_topic.test.id]
  }
}

